/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>销量排行, 由总平台设置管理.</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>

    <div class="mDiv">
      <div class="ftitle">
        <h3>销量排行列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <div class="searchbox">
        <el-cascader
          v-model="categoryId"
          :options="goodsCategoryTree"
          :props="{label: 'name', value: 'id',expandTrigger: 'hover',checkStrictly: true}"
          clearable
          size="small"
          placeholder="所有分类"
        />
        <el-date-picker
          size="small"
          v-model="dateRange"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="下单日期起始"
          end-placeholder="下单日期截止"
        ></el-date-picker>
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getPage()"
        >搜索</el-button>
      </div>
    </div>
    <div class="tablebox">
      <el-table :data="tableData" style="width: 100%;">
        <el-table-column label="排行" type="index" :index="indexMethod" width="100"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="360" show-overflow-tooltip></el-table-column>
        <el-table-column label="货号" prop="goods_sn" width="260"></el-table-column>
        <el-table-column label="销售量" prop="sale_num" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="销售额" prop="sale_amount" width="100"></el-table-column>
        <el-table-column label="均价">
          <template slot-scope="scope">{{scope.row.sale_amount / scope.row.sale_num | numFilter}}</template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getSaleTopPage, getGoodsCategoryTree } from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 20,
      dateRange: [
        new Date(new Date().toLocaleDateString()).getTime() -
          6 * 60 * 60 * 24 * 1000,
        new Date(new Date().toLocaleDateString()).getTime() +
          (1 * 60 * 60 * 24 * 1000 - 1000)
      ],
      query: {},
      goodsCategoryTree: [],
      categoryId: []
    };
  },
  components: {},
  created() {
    this.getPage();
    this.getGoodsCategoryTree();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      this.query.p = this.currentPage;
      this.query.size = this.pageSize;
      this.query.cat_id = this.categoryId[this.categoryId.length - 1];
      this.query.start_time = parseInt(this.dateRange[0].valueOf() / 1000);
      this.query.end_time = parseInt(this.dateRange[1].valueOf() / 1000);
      getSaleTopPage(that.query).then(function(res) {
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    getGoodsCategoryTree() {
      var that = this;
      getGoodsCategoryTree({
        levels: "1,2,3"
      }).then(function(res) {
        that.goodsCategoryTree = res;
      });
    },
    refresh() {
      this.getList();
      this.$message({
        type: "success",
        message: "刷新成功！"
      });
    },
    indexMethod(index) {
      let curpage = this.currentPage; //单前页码
      let limitpage = this.pageSize; //每页条数
      return index + 1 + (curpage - 1) * limitpage;
    }
  },
  filters: {
    numFilter(value) {
      let realVal = "";
      if (!isNaN(value) && value !== "") {
        // 截取当前数据到小数点后两位
        realVal = parseFloat(value).toFixed(2);
      } else {
        realVal = "--";
      }
      return realVal;
    }
  }
};
</script>

<style scoped>
</style>
